import {BaseCodeGenerator} from "../BaseCodeGenerater";
import {ConfigDataEntity} from "../../../generate/entitas";
import path = require("path");
import * as fs from "fs";
import {CellData} from "../../CellData";
import excelConfig from "../../../ExcelConfig";
import jsonTool from "../../../3rd/json";
import {PlatForm} from "../../PlatForm";
let ejs = require('ejs')
__dirname = __dirname.replace("/dist", "/src")

export class TsGenerator extends BaseCodeGenerator{
	jsPath : string

	constructor() {
        super();
		this.dirPath = __dirname
		this.init()
		this.outPath = excelConfig.tempProjectRoot + this.className + "PB_Desc"
		this.jsPath = this.dirPath + "temp/PB_Js"
	}

	onBeforeGenCode() {

	}

	onStartGeCode(entity: ConfigDataEntity) {

		this.callListAction(entity.onGenerateCodeAction.value, entity)
		this.generateCodeWithEntity(entity, "ConfigMgr")
		this.generateCodeWithEntity(entity, "Config")
	}

	generateCodeWithEntity(entity : ConfigDataEntity, filePath : string)
	{
		let file = fs.readFileSync(path.join(this.makoPath, filePath + ".ejs"), 'utf-8')
		let ret : string = ejs.render(file,
			{
				"headList" : entity.memberHeadDataList.value,
				"className" : entity.sheetName.value + "Config",
				"columnEntityList" : entity.columnEntityList.value,
				"initMethod" : entity.indexInitMethodNameDict.value,
			}
		)

		ret = ret.replace(/&lt;/g, "<").replace(/&gt;/g, ">")
			.replace(/&amp;lt/g,"<").replace(/&amp;gt/g,">")
			.replace(/&amp;lt;/g,"<").replace(/&amp;gt;/g,">")
			.replace(/&#34;/g,'"')
		fs.writeFileSync(path.join(this.tempScriptPath, entity.sheetName.value + filePath + ".ts") , ret)
	}

	onStartGenData(entity: ConfigDataEntity) {
		let headData = new Map()
		headData.set('headInfo', [])
		entity.replaceHeadInfo(headData)
		this.genDataInit()
		this.generateData(entity)
	}

	genDataInit(){

	}

	generateData(entity: ConfigDataEntity){
		var headList = entity.memberHeadDataList.value
		var list = entity.rowCellList.value
		let valueList = {}
		for (let r = 0; r < list.length; r++) {
			var rowData = list[r]
			let v = {}
			for (let c = 0; c < headList.length; c++) {
				var head = headList[c]
				var cellData : CellData = rowData[c]
				v[head.name] = cellData.parseVal
			}
			valueList[rowData[0].parseVal] = v
		}
		var jsonData = jsonTool.stringify({head : entity.headInfo.value, value : valueList})
		let fileName = path.join(this.tempDataPath , entity.sheetName.value + "Config.bytes")
		// console.log(jsonData)
		fs.writeFileSync(fileName, jsonData)
	}

	onAfterGenData(entity: ConfigDataEntity) {
		this.callListAction(entity.onGenerateHeadDataAction.value, entity)
	}

	genConfigHelper(){
		let filePath = "ConfigHelper"
		let allEntity = context.GetEntitiesByExportPlatformIndex(true, PlatForm.All)
		let classNameList = []
		allEntity.forEach((value, index) => {
			classNameList.push(value.sheetName.value)
		})
		let file = fs.readFileSync(path.join(this.makoPath, filePath + ".ejs"), 'utf-8')
		let ret : string = ejs.render(file,
			{
				"classNameList" : classNameList,
			}
		)

		ret = ret.replace(/&lt;/g, "<").replace(/&gt;/g, ">")
			.replace(/&amp;lt/g,"<").replace(/&amp;gt/g,">")
			.replace(/&amp;lt;/g,"<").replace(/&amp;gt;/g,">")
			.replace(/&#34;/g,'"')
		fs.writeFileSync(path.join(this.tempScriptPath, filePath + ".ts") , ret)

	}

	onGenCodeEnd(){
		this.genConfigHelper()
		if (fs.existsSync(excelConfig.outScriptPath)){
			fs.rmdirSync(excelConfig.outScriptPath,{
				recursive: true  //是否递归,默认false
			})
		}
		fs.mkdirSync(excelConfig.outScriptPath)
		utils.copyFolder(this.tempScriptPath, excelConfig.outScriptPath)
	}

	onGenDataEnd() {
		if (fs.existsSync(excelConfig.outDataPath)){
			fs.rmdirSync(excelConfig.outDataPath,{
				recursive: true  //是否递归,默认false
			})
		}
		fs.mkdirSync(excelConfig.outDataPath, {
			recursive: true  //是否递归,默认false
		})
		utils.copyFolder(this.tempDataPath, excelConfig.outDataPath)
	}

	onAfterGenCode(entity: ConfigDataEntity) {

	}
}